package us.mooresr.webtroopmanager.client.ui;

import java.util.List;

import us.mooresr.webtroopmanager.client.place.DashboardPlace;
import us.mooresr.webtroopmanager.client.place.EditVehiclePlace;
import us.mooresr.webtroopmanager.client.ui.cells.VehicleCell;
import us.mooresr.webtroopmanager.shared.Privilege;
import us.mooresr.webtroopmanager.shared.VehicleSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class VehicleListViewImpl extends Composite implements VehicleListView {
	private Presenter presenter;
	private CellList<VehicleSummary> cellList;
	private ListDataProvider<VehicleSummary> dataProvider;
	private VehicleSummary selectedVehicle;
	
	private static vehicleListUiBinder uiBinder = GWT.create(vehicleListUiBinder.class);
	
	interface vehicleListUiBinder extends UiBinder<Widget, VehicleListViewImpl> {
		
	}
	
	@UiField
	Label statusLabel;
	
	@UiField
	Button addButton;
	
	@UiField
	Button editButton;
	
	@UiField
	Button deleteButton;
	
	@UiField
	Button backButton;
	
	@UiField
	ScrollPanel scrollPanel;
	
	@UiHandler("addButton")
	void onAddClick(ClickEvent event) {
		presenter.goTo(new EditVehiclePlace(null));
	}
	
	@UiHandler("editButton")
	void onEditClick(ClickEvent event) {
		presenter.goTo(new EditVehiclePlace(selectedVehicle.getId()));
	}
	
	@UiHandler("deleteButton")
	void onDeleteClick(ClickEvent event) {
		presenter.deleteVehicle(selectedVehicle);
	}
	
	@UiHandler("backButton")
	void onBackClick(ClickEvent event) {
		presenter.goTo(new DashboardPlace());
	}
	
	public VehicleListViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		// TODO:  Check that the status reporting still works right
		
		ProvidesKey<VehicleSummary> keyProvider = new ProvidesKey<VehicleSummary>() {
			public Object getKey(VehicleSummary item) {
				return (item == null) ? null : item.getId();
			}
		};
		cellList = new CellList<VehicleSummary>(new VehicleCell(), keyProvider);

		cellList.addStyleName("listControl");
		
		final SingleSelectionModel<VehicleSummary> selectionModel = new SingleSelectionModel<VehicleSummary>(keyProvider);
		cellList.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				selectedVehicle = selectionModel.getSelectedObject();
				if (selectedVehicle == null) {
					// Disable buttons that act on a selected event
					editButton.setEnabled(false);
					deleteButton.setEnabled(false);
	
				} else {
					// Enable buttons that act on a selected event
					if (presenter.isAllowed(Privilege.MODIFY_VEHICLE, selectedVehicle.getId())){
						editButton.setEnabled(true);
					}
					if (presenter.isAllowed(Privilege.DELETE_VEHICLE, selectedVehicle.getId())) {
						deleteButton.setEnabled(true);
					}

				}
				
				
			}
			
		});
		
		dataProvider = new ListDataProvider<VehicleSummary>();
		dataProvider.addDataDisplay(cellList);
		
		scrollPanel.setWidget(cellList);
		
		deleteButton.setEnabled(false);
		editButton.setEnabled(false);

	}


	@Override
	public void setData(List<VehicleSummary> data) {
		List<VehicleSummary> providerList = dataProvider.getList();
		providerList.clear();
		for (VehicleSummary v : data) {
			providerList.add(v);
		}
		cellList.setPageSize(data.size());

	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
		if (presenter.hasPermission(Privilege.ADD_VEHICLE)) {
			addButton.setEnabled(true);
		} else {
			addButton.setEnabled(false);
		}

	}
	

	@Override
	public void addVehicle(VehicleSummary vehicleSummary) {
		List<VehicleSummary> providerList = dataProvider.getList();
		providerList.add(vehicleSummary);
		
		// TODO: Resort the list?
		
		cellList.setPageSize(providerList.size());
		
	}


	@Override
	public void removeVehicle(Long vehicleId) {
		List<VehicleSummary> providerList = dataProvider.getList();
		for (int i=0; i<providerList.size(); i++) {
			VehicleSummary vs = providerList.get(i);
			if (vs.getId().equals(vehicleId)) {
				providerList.remove(i);
				break;
			}
		}
		
	}


	@Override
	public void setStatus(String status) {
		statusLabel.setText(status);
		
	}

}
